package com.qd.system.treecl;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.qd.system.mapper.TreeMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.toList;

/**
 * 〈一句话功能简述〉<br>
 *
 * @author sjk
 * @since 1.0.0
 */
@Repository
public class TreeClRepository {
    @Resource
    private TreeMapper treeMapper;

    /**
     * 查询原始表数据
     *
     * @param dto 查询参数
     * @return 数据列表，id，pid，name
     */
    public List<TreeModel> getAllTree(TreeClDTO dto) {
        final List<Map<String, String>> list = treeMapper.getAllTree(dto);
        if (null == list || list.isEmpty()) {
            return Collections.emptyList();
        }
        final List<TreeModel> treeModels = new ArrayList<>(list.size());
        list.forEach(stringStringMap -> treeModels.add(buildTree(dto, stringStringMap)));
        return treeModels;
    }

    /**
     * 查询原始表数据，按id查询，单个
     *
     * @param dto 查询参数
     * @return 数据列表，id，pid，name
     */
    public TreeModel getTree(TreeClDTO dto) {
        final Map<String, String> map = treeMapper.getTree(dto);
        if (null == map || map.isEmpty()) {
            return null;
        }
        return buildTree(dto, map);
    }

    /**
     * 查询cl表数据
     *
     * @param dto 查询参数
     * @return 数据列表，id，pid，name
     */
    public List<TreeClModel> getClTreeList(TreeClDTO dto) {
        final List<Map<String, Object>> list = treeMapper.getClTreeList(dto);
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        return list.stream().map(stringStringMap -> buildTreeCl(dto, stringStringMap)).collect(toList());
    }

    /**
     * 查询cl表数据,按父级id查询
     *
     * @param dto 查询参数
     * @return 数据列表，id，pid，name
     */
    public List<TreeClModel> getClTreeListByPid(TreeClDTO dto) {
        final List<Map<String, Object>> list = treeMapper.getClTreeListByPid(dto);
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        return list.stream().map(stringStringMap -> buildTreeCl(dto, stringStringMap)).collect(toList());
    }

    /**
     * 删除cl表数据
     *
     * @param dto 按id删除
     * @return 删除成功条数
     */
    public boolean delete(TreeClDTO dto) {
        return SqlHelper.retBool(treeMapper.deleteTreeCl(dto));
    }

    /**
     * 删除cl表数据
     *
     * @param dto 全量删除
     * @return 删除成功条数
     */
    public boolean deleteTreeClAll(TreeClDTO dto) {
        return SqlHelper.retBool(treeMapper.deleteTreeClAll(dto));
    }

    /**
     * 删除cl表数据
     *
     * @param dto 参数
     * @param ids 需要删除逇id集合
     * @return 删除成功条数
     */
    public boolean deleteTreeClByIds(TreeClDTO dto, String[] ids) {
        return SqlHelper.retBool(treeMapper.deleteTreeClByIds(dto, ids));
    }

    /**
     * 批量添加
     *
     * @param dto  参数
     * @param list 需要添加的数据
     * @return 成功条数
     */
    public boolean saveBatchTreeCl(TreeClDTO dto, List<TreeClModel> list) {
        final int i = treeMapper.saveBatchTreeCl(dto, list);
        return i == list.size();
    }

    /**
     * 批量更新
     *
     * @param dto  参数
     * @param list 需要更新的数据
     * @return 成功条数
     */
    public boolean updateBatchTreeCl(TreeClDTO dto, List<TreeClModel> list) {
        final int i = treeMapper.updateBatchTreeCl(dto, list);
        return i == list.size();
    }

    private TreeModel buildTree(TreeClDTO dto, Map<String, String> map) {
        if (null != map && !map.isEmpty()) {
            final TreeModel treeModel = new TreeModel();
            final String id = map.get(dto.getTableColumnId());
            final String pid = map.get(dto.getTableColumnPid());
            final String name = map.get(dto.getTableColumnName());
            treeModel.setId(id);
            treeModel.setPid(pid);
            treeModel.setName(name);
            return treeModel;
        }
        return null;
    }

    private TreeClModel buildTreeCl(TreeClDTO dto, Map<String, Object> map) {
        if (null != map && !map.isEmpty()) {
            final TreeClModel treeClModel = new TreeClModel();
            final String id = map.get(dto.getClTableColumnId()).toString();
            final String cid = map.get(dto.getClPrimaryKey()).toString();
            final String pid = map.get(dto.getClTableColumnPid()).toString();
            final String name = map.get(dto.getClTableColumnName()).toString();
            final String pName = map.get(dto.getClTableColumnParentName()).toString();
            final String distance = map.get(dto.getClTableColumnDistance()).toString();
            treeClModel.setCid(cid);
            treeClModel.setId(id);
            treeClModel.setPid(pid);
            treeClModel.setName(name);
            treeClModel.setPName(pName);
            treeClModel.setDistance(Integer.parseInt(distance));
            return treeClModel;
        }
        return null;
    }
}